#coding: utf-8
import hashlib, random, time, datetime, sys, traceback


def smd5(s):
    m = hashlib.md5()
    if isinstance(s, str):
        s = s.encode("utf-8")
    m.update(s)
    return m.hexdigest()


def ssha1(s):
    return hashlib.sha1(s).hexdigest()


'''
将dict的key，value转换成utf-8
'''
def dictkv2str(datas):
    rt = {}
    for k in datas:
        nv = datas[k].encode('utf-8') if isinstance(datas[k], str) else datas[k]
        nk = k.encode('utf-8') if isinstance(k, str) else k
        rt[nk] = nv
    return rt
    

'''
类型检查
'''
def paramChecker(func):

    def _paramChecker(*args, **kargs):
        # do check
        for k in kargs:
            v = kargs[k]

            if k == 'key':
                if not (v and len(v) == 32):
                    return False
            if k == 'appid':
                if not (v and len(v) == 18):
                    return False
            if k == 'openid':
                if not (v and len(v) == 28):
                    return False

        ret = func(*args, **kargs)
        return ret

    return _paramChecker

'''
处理返回值
'''
def return_fail(data=''):
    return {'errcode': 40001, 'data':data}

def get_date(_format="%Y-%m-%d"):
    return datetime.datetime.now().strftime(_format)

def return_success(data=''):
    return {'errcode': 0, 'data':data}
